/*
 * search.c
 *
 *  Created on: 2010-10-2
 *      Author: jack
 */

#include <gtk-2.0/gtk/gtk.h>
#include <libglade-2.0/glade/glade.h>
#include <glib-2.0/glib.h>
#include <stdlib.h>
#include "../include/gstreamer.h"
#include "../include/xml.h"
#include "../include/structs.h"

enum
{
  COLUMN1 = 0,
  COLUMN2,
  COLUMN3,
  COLUMN4,
  NUM_COLS
} ;


GtkBuilder *builder;
GtkWidget *mainmenu,*search_button,*start_player_button,*keyword_entry,*result_box,*exit_button;
searchresult *head;
static char *keywords;


static GtkTreeModel * CreateTree(searchresult *head)
{
	GtkListStore *liststore;
	GtkTreeIter toplevel;

	searchresult * current=head->next;

	liststore = gtk_list_store_new(NUM_COLS,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING,G_TYPE_STRING);


	while(current!=NULL)
	{
		gtk_list_store_append(liststore, &toplevel);
		gtk_list_store_set(liststore, &toplevel,COLUMN1,current->title,COLUMN2,current->artist,COLUMN3,current->type,COLUMN4,current->id,-1);

		current=current->next;
	}

	return GTK_TREE_MODEL(liststore);
}

static int create_view_and_model(searchresult *head,GtkTreeView *treeview)
{
  GtkTreeViewColumn *col;
  GtkCellRenderer *renderer;
  GtkTreeView *view;
  GtkTreeModel *model;

  view = treeview;

  /*移除所有已存在的列!!!*/
  GtkTreeViewColumn *tempcol;
  int temp;
  for(temp=0;temp<NUM_COLS;temp++)
	  if((tempcol=gtk_tree_view_get_column(view,0))!=NULL)
		  gtk_tree_view_remove_column(view,tempcol);

  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "Title");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);

  renderer = gtk_cell_renderer_text_new();
  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer,
      "text", COLUMN1);

  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "Artist");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);


  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer,
      "text", COLUMN2);

  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "Type");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);


  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer,
      "text", COLUMN3);

  col = gtk_tree_view_column_new();
  gtk_tree_view_column_set_title(col, "ID");
  gtk_tree_view_append_column(GTK_TREE_VIEW(view), col);
  gtk_tree_view_column_set_visible(col,FALSE);/*此行存储ID信息，但是设置为对用户不可见!!!*/

  gtk_tree_view_column_pack_start(col, renderer, TRUE);
  gtk_tree_view_column_add_attribute(col, renderer,
      "text", COLUMN4);

  model = CreateTree(head);
  gtk_tree_view_set_model(GTK_TREE_VIEW(view), model);
  g_object_unref(model);

  return 0;
}


int search_cb(GtkWidget *widget,gpointer data)
{
	keywords=gtk_entry_get_text(keyword_entry);
	head=get_search_result(keywords);
	create_view_and_model(head,(GtkTreeView *)result_box);
	gtk_widget_set_sensitive(widget,FALSE);
	return 0;
}

int start_player_cb(GtkWidget *widget,gpointer data)
{
	GtkTreeSelection *selection=gtk_tree_view_get_selection((GtkTreeView *)result_box);
	GtkTreeIter iter;
	GtkTreeModel *model;
	songinfo *info;
	char *id;
	char *type;
	if(gtk_tree_selection_get_selected(selection,&model,&iter))
	{
		gtk_tree_model_get(model,&iter,COLUMN3,&type,COLUMN4,&id,-1);
		if(strcmp(type,"song")==0)
		{
			char query[100];/*TODO:size?*/
			sprintf(query,"pw=Shit&action=info&type=song&id=%s",id);
			info=get_song_info(query);
		}
		else
		{
			char query[100];/*TODO:size?*/
			sprintf(query,"pw=Shit&action=info&type=album&id=%s",id);
			info=get_song_info(query);
		}
		init_pipeline(info);
		start_player(info);
	}

	return 0;
}

int keyword_change_cb(GtkWidget *widget,gpointer data)
{
	gtk_widget_set_sensitive(search_button,TRUE);
	return 0;
}

int show_search_box()
{
	gtk_init(NULL,NULL);

	builder = gtk_builder_new();
	gtk_builder_add_from_file(builder, "../glade/searchbox.glade", NULL);
	mainmenu = GTK_WIDGET(gtk_builder_get_object(builder, "search_box"));
	search_button=GTK_WIDGET(gtk_builder_get_object(builder, "search_button"));
	exit_button=GTK_WIDGET(gtk_builder_get_object(builder, "exit_button"));
	start_player_button=GTK_WIDGET(gtk_builder_get_object(builder, "start_player_button"));
	keyword_entry=GTK_WIDGET(gtk_builder_get_object(builder, "keyword_entry"));
	result_box=GTK_WIDGET(gtk_builder_get_object(builder, "result_box"));
	g_signal_connect( G_OBJECT(mainmenu), "destroy",G_CALLBACK(gtk_main_quit),NULL);
	g_signal_connect( G_OBJECT(exit_button), "clicked",G_CALLBACK(gtk_main_quit),NULL);
	g_signal_connect( G_OBJECT(search_button), "clicked",G_CALLBACK(search_cb),NULL);
	g_signal_connect( G_OBJECT(start_player_button), "clicked",G_CALLBACK(start_player_cb),NULL);
	g_signal_connect( G_OBJECT(keyword_entry), "changed",G_CALLBACK(keyword_change_cb),NULL);
	gtk_builder_connect_signals(builder, NULL);
	gtk_widget_show(mainmenu);
	gtk_main();
	g_object_unref(G_OBJECT(builder));
	return 0;
}
